// utils/test-api.js
// 用于测试收货地址API接口的简单工具

import shippingAddressApi from './api/shippingAddress.js'

/**
 * 测试收货地址API接口
 */
export const testShippingAddressApi = {
  /**
   * 测试获取用户地址列表
   */
  async testGetUserAddresses() {
    console.log('测试获取用户地址列表...')
    try {
      const result = await shippingAddressApi.getUserAddresses()
      console.log('获取地址列表结果:', result)
      return result
    } catch (error) {
      console.error('获取地址列表失败:', error)
      return null
    }
  },

  /**
   * 测试新增地址
   */
  async testAddAddress() {
    console.log('测试新增地址...')
    try {
      const addressData = {
        receiver_name: '测试用户',
        receiver_phone: '13800138000',
        province: '广东省',
        city: '深圳市',
        district: '南山区',
        street: '测试地址123号',
        is_default: 0
      }
      const result = await shippingAddressApi.addAddress(addressData)
      console.log('新增地址结果:', result)
      return result
    } catch (error) {
      console.error('新增地址失败:', error)
      return null
    }
  },

  /**
   * 测试获取地址详情
   */
  async testGetAddressDetail(id) {
    console.log('测试获取地址详情...')
    try {
      const result = await shippingAddressApi.getAddressDetail(id)
      console.log('获取地址详情结果:', result)
      return result
    } catch (error) {
      console.error('获取地址详情失败:', error)
      return null
    }
  },

  /**
   * 测试修改地址
   */
  async testUpdateAddress(id) {
    console.log('测试修改地址...')
    try {
      const addressData = {
        id: id,
        receiver_name: '修改后的用户',
        receiver_phone: '13900139000',
        province: '广东省',
        city: '深圳市',
        district: '福田区',
        street: '修改后的地址456号',
        is_default: 1
      }
      const result = await shippingAddressApi.updateAddress(addressData)
      console.log('修改地址结果:', result)
      return result
    } catch (error) {
      console.error('修改地址失败:', error)
      return null
    }
  },

  /**
   * 测试设置默认地址
   */
  async testSetDefaultAddress(id) {
    console.log('测试设置默认地址...')
    try {
      const result = await shippingAddressApi.setDefaultAddress(id)
      console.log('设置默认地址结果:', result)
      return result
    } catch (error) {
      console.error('设置默认地址失败:', error)
      return null
    }
  },

  /**
   * 测试删除地址
   */
  async testDeleteAddress(id) {
    console.log('测试删除地址...')
    try {
      const result = await shippingAddressApi.deleteAddress(id)
      console.log('删除地址结果:', result)
      return result
    } catch (error) {
      console.error('删除地址失败:', error)
      return null
    }
  },

  /**
   * 运行所有测试
   */
  async runAllTests() {
    console.log('开始运行收货地址API测试...')
    
    // 1. 获取地址列表
    const listResult = await this.testGetUserAddresses()
    
    // 2. 新增地址
    const addResult = await this.testAddAddress()
    let newAddressId = null
    
    if (addResult && addResult.success && addResult.data) {
      newAddressId = addResult.data.id || addResult.data
    }
    
    if (newAddressId) {
      // 3. 获取地址详情
      await this.testGetAddressDetail(newAddressId)
      
      // 4. 修改地址
      await this.testUpdateAddress(newAddressId)
      
      // 5. 设置默认地址
      await this.testSetDefaultAddress(newAddressId)
      
      // 6. 删除地址
      await this.testDeleteAddress(newAddressId)
    }
    
    console.log('收货地址API测试完成')
  }
}

export default testShippingAddressApi 